package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.PhyUserService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.PhyUser;
import com.gxa.jjty.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxa.jjty.core.util.MD5Util;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @ClassName PhyUserController
 * @Author: ws
 * @date 2021/12/2 15:18
 */
@Log4j2
@RestController
@RequestMapping("/phyUser")
@Api(tags = "陪护员用户的接口")

public class PhyUserController {

    @Autowired
    PhyUserService phyUserService;

    @PostMapping("/getALL")
    @ApiOperation("分页全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "每页记录数")
    })
    public Map getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size) throws IllegalAccessException {

        PageInfo<PhyUser> pageInfo = phyUserService.selectByPageAnd(page, size);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询陪护员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号")
    })
    public Map getById(@RequestParam("id") Long id){

        PhyUser phyUser = phyUserService.selectById(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(phyUser);

        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("更新陪护员用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "area", value = "区域",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "payee", value = "收款人",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "bank", value = "开户行",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "bankCard", value = "卡号",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",paramType = "query",dataType = "String"),

    })
    public Map update( @ApiIgnore @RequestBody PhyUser phyUser) throws IllegalAccessException {

        phyUserService.updateById(phyUser);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @PostMapping("/insert")
    @ApiOperation("添加陪护员用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "area", value = "区域",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "payee", value = "收款人",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "bank", value = "开户行",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "bankCard", value = "卡号",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",paramType = "query",dataType = "String"),
    })
    public Map insert(@ApiIgnore @RequestBody PhyUser phyUser) throws IllegalAccessException {

        phyUserService.insert(phyUser);
        String hName=phyUser.getName().substring(phyUser.getName().length()-2,phyUser.getName().length());
        if(phyUser.getName().length()>=3){
            phyUser.setHeadName(hName);
        }
        phyUser.setRegTime(new Timestamp(System.currentTimeMillis()));
        phyUser.setPassword(MD5Util.encode("123456"));

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除陪护员用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号id")
    })
    public Map delete(@RequestParam("id") Long id)  {

        phyUserService.deleteById(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @PostMapping("/restPassword")
    @ApiOperation("重置密码")
    public Map restPassword(){

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("密码重置成功");

        return jsonResult.getDataMap();
    }

    @PostMapping("/getALLBy")
    @ApiOperation("分页全/带条件模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页记录数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "from", value = "起始时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "to", value = "结束时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "userName", value = "用户名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "area", value = "区域",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",paramType = "query",dataType = "String"),
    })
    public Map getByTimeAnd(@RequestParam("page") Integer page, @RequestParam("size") Integer size,
                            @RequestParam(value = "from",required = false) String from,
                            @RequestParam(value = "to",required = false) String to,
                            @ApiIgnore PhyUser phyUser) throws IllegalAccessException {

        Timestamp newFrom = null;
        Timestamp newTo = null;
        if (StringUtil.isEmpty(from)) {
            newFrom = new Timestamp(0);
        }else {
            newFrom = Timestamp.valueOf(from);
        }
        if (StringUtil.isEmpty(to)) {
            newTo = new Timestamp(System.currentTimeMillis());
        }else {
            newTo = Timestamp.valueOf(to);
        }

        PageInfo<PhyUser> pageInfo = phyUserService.selectByTimeAnd(page, size, newFrom, newTo, phyUser);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }



}
